
/**
 *  Check field is empty, max length, min lengh
 */
jQuery.fn.isExist = function() {
    return jQuery(this).length > 0;
}
jQuery.fn.include = function(url) {
    jQuery(this).load("app/"+url+".html");
}

jQuery.fn.isNullOrEmpty = function() {
    if (jQuery(this).val() === null || $.trim(jQuery(this).val()) === "") {
        return true;
    } else {
        return false;
    }
}

jQuery.fn.isEmail = function() {
    var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return regex.test(jQuery(this).val());
}

jQuery.fn.stringIsValidate = function(minleng, maxlenth) {
    if (jQuery.trim(jQuery(this).val()).length < minleng || jQuery.trim(jQuery(this).val()).length > maxlenth) {
        jQuery(this).focus();
        return false;
    } else {
        return true;
    }
}
function redirect_state(url_state) {
    window.location.href = "#" + url_state.replace(".html", "");
}

jQuery.fn.preload = function() {
    $(this).html("<div class='areaLoading'>Loading...</div>");
    $(this).css({
        "position":"relative"
    });
}


String.prototype.replaceAll = function(
    strTarget, // The substring you want to replace
    strSubString // The string you want to replace in.
    ) {
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);
    while (intIndexOfMatch !== -1) {
        strText = strText.replace(strTarget, strSubString)
        intIndexOfMatch = strText.indexOf(strTarget);
    }
    return(strText);
}
function get_param(name) {
    var url = window.location.hash;
    var index = url.indexOf("?");
    var maxlen = url.length;
    
    if (index > 0) {//Kiem tra co param
        //Xu ly chuoi lan 1
        url = url.substring(index + 1, maxlen);
        var param_index = url.indexOf(name);
        if(param_index >= 0){//Kiem tra lan 2, ton tai param can tim
            url = url.substring(param_index,maxlen);
            //Xu ly chuoi lan 2
            maxlen = url.indexOf("&")>0?url.indexOf("&"):maxlen;
            url = url.substring(0,maxlen).replace(name + "=", "");
        }else{
            url="";
        }
    } else {
        url = "";
    }
    return url;
}

function CurrencyFormatted(n) {
    return (Math.round(n * 100) / 100).toLocaleString();
}

/**
 *  Hàm tải nội dung trong trang
 */
function loadContent(url, params, container) {
    $.get(url, params, function(data) {
        container.html(data);
    }).done(function() {
        $(".tabs_item").removeClass("item_active");
        container.addClass("item_active");
    }).error(function() {
        //container.find(".loading").hide();
        //containerWaring.pushWarning("Lỗi tải nội dung.", "alert-error", "");
        });
}

snapper = null;
function swipeNav(){
     
    /*
     *@Swipe Nav
     */

    snapper = new Snap({
        element: document.getElementById('page'),
        maxPosition: 285,
        minPosition: -285,
        addBodyClasses: false
    });
    
    $("#theloai").tap(function(){
        var state = snapper.state();
        if(state.state=="closed"){
            snapper.open('left'); 
            $(this).addClass("active");
        }else{
            $(this).removeClass("active");
            snapper.close();
        }
    });
}



function normalScroll(selector) {
    var myScroll = new iScroll(selector, {
        scrollbarClass: 'myScrollbar'
    });
}


/*
 *@Popup and dialog 
 */
function POPUP() {
    this.show = function(options){
        if($(".popup_overlay").length==0){
            var popup =  '<div class="popup_overlay">'
            + '<div class="popup">'
            + '<div class="popup_header">'+options.title+'</div>'
            + '<div class="popup_content">'
            + '<ul>'
            +  '<li><a href="'+options.distance_url+'">Xem bản đồ '+options.distance_user+'</a></li>'
            +  '<li><a href="'+options.user_url+'">Trang cá nhân</a></li>'
            +  '<li><a id="wg_url_chat" href="">Gửi tin nhắn</a></li>'
            +  '<li><a href="'+options.chat_invi_url+'">Chat ẩn danh</a></li>'
            + '</ul>'
            + '</div>'
            + '</div>'
            +' </div>';
            $("body").prepend(popup);
        }
     
        
    }
    
    this.show1 = function(options){
        var popup =  '<div class="popup_overlay">'
        + '<div class="popup">'
        + '<div class="popup_header">'+options.title+' <button class="close"><span class="icon-close"></span></button></div>'
        + '<div class="popup_content">'
        + '<ul>'
        +  '<li><a href="'+options.distance_url+'">Xem bản đồ '+options.distance_user+'</a></li>'
        +  '<li><a href="'+options.user_url+'">Trang cá nhân </a></li>'
        +  '<li><a id="wg_url_ok_datting" data-userid="'+options.userid+'" data-touserid="'+options.touserid+'">Nhận lời hẹn hò </a></li>'
        +  '<li><a id="wg_url_chat">Gửi tin nhắn </a></li>'
        +  '<li><a href="'+options.chat_invi_url+'">Chat ẩn danh</a></li>'
       
        + '</ul>'
        + '</div>'
        + '</div>'
        +' </div>';
        $("body").prepend(popup);
        confirmDatting();
    }
    
    this.close = function(){
        $(".popup_overlay").remove(); 
    }
    
    /*
     *@Dialog confirm 
     */
    this.dialog = function(options){
      
        var buttons = '';
        var fotter = '';
        var length = 0;
        if(options.buttons!=undefined){
            $.each(options.buttons,function(i,index){
                length++;
                buttons+= "<button id='"+index.id+"'>"+index.text+"</button>";
            });
            fotter =  '<div class="popup_footer popup_footer_g'+length+'">'+buttons+'</div>';
        }
        var html = '<div id="'+options.id+'" class="popupScreen dialog" style="display: block;">'
        + '<div class="popupInside">'
        +   ' <div class="boxItems" style="height:'+options.height+'">'
        +        ' <div class="popup_header">'+options.header+'</div>'
        +        ' <div class="boxContent"><p>'
        +options.text
        +        '</p></div>'
        +        fotter
        +   '   </div>'
        +  ' </div>'
        + '</div>';
        $("body").prepend(html);
        
 
        $(".dialog .close,#btnCancel").tap(function(){
            new POPUP().closeDialog();
        });
    
    }
    
    this.closeDialog = function(){
        $(".dialog").remove();
    }
    
    /*
     *@Usage dialog
     */
    /*new POPUP().dialog({
                    id:"test",
                    header:"Test",
                    text:"Hello I just wanna test something Hello I just wanna test something Hello I just wanna test something Hello I just wanna test something",
                    height:"40%",
                    buttons:{"btn1":{id:"btnOk",text:"Đồng ý"},"btn2":{id:"btnCancel",text:"Thoát"},"btn3":{id:"btnKobiet",text:"Ko biet"}}
    });*/
    
    this.toats = function(text,width){
        $("body").prepend('<div class="toast" style="width:'+width+'px">'+text+'</div>');
        setTimeout(function(){
            $(".toast").css({
                "opacity":"0",
                "transition":"all ease .8s",
                "-webkit-transition":"all ease .8s",
                "-ms-transition":"all ease .8s",
                "-o-transition":"all ease .8s"
            });
        },3000);
        setTimeout(function(){
            $(".toast").remove();
        },3800);
    }
  
}
